﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员月Tick盈利，亏损情况
    /// </summary>
    public class ServiceOperatorMonthTradeInfo : Benefit.Interface.View.IOperatorMonthTradeInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorMonthTradeInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 根据关键字查询交易员当天分时情况
        /// </summary>
        /// <param name="pdate">查询日期</param>
        /// <param name="operatorid">操作员编号</param>
        /// <param name="key">profit？手续费？</param>
        public List<OperatorMonthTradeInfoData> GetOperatorMonthTradeInfo(int year, int month, int operatorid, string key)
        {
            List<OperatorMonthTradeInfoData> data = new List<OperatorMonthTradeInfoData>();
            var query = db.TradeHistorys.Where(a => a.YPdate == year).Where(a => a.MPdate == month);
            Interface.View.IOperatorDayTradeInfo iodti = new View.ServiceOperatorDayTradeInfo(db);
            foreach (Models.Sys.TradeHistory t in query)
            {
                List<OperatorDayTradeInfoData> dayInfos = iodti.GetOperatorDayTradeInfo(t.Id, operatorid, key).Datas;
                foreach (OperatorDayTradeInfoData d in dayInfos)
                {
                    var queryExist = data.Where(a => a.Clock == d.Clock);
                    if (queryExist.Count() > 0)
                    {
                        queryExist.First().BillCount += d.BillCount;
                        queryExist.First().Percent += d.Percent;
                        queryExist.First().SumValue += d.SumValue;
                    }
                    else
                    {
                        OperatorMonthTradeInfoData _data = new OperatorMonthTradeInfoData();
                        _data.BillCount = d.BillCount;
                        _data.Clock = d.Clock;
                        _data.OperatorId = operatorid;
                        _data.Percent = d.Percent;
                        _data.SumValue = d.SumValue;
                        data.Add(_data);
                    }
                }
            }
            return data;
        }
    }

   
}
